<template>
	<view class="page">
		<!-- 订单tab -->
		<view class="order-tab">
			<view class="tab" :class="{'action':OrderType==0}" @click="onOrderTab(0)">
				<text>全部</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==4}" @click="onOrderTab(4)">
				<text>操盘中</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==2}" @click="onOrderTab(2)">
				<text>审核中</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==3}" @click="onOrderTab(3)">
				<text>未通过</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==5}" @click="onOrderTab(5)">
				<text>已结束</text>
				<text class="line"></text>
			</view>
		</view>
		<!-- 订单列表 -->
		<view class="order-list">
			<view class="order-li" v-for="(item,index) in PeiziList" :key="index">
				<view class="order-number" @click="goDetails(token,item.id)">
					<view class="number">
						<text>申请单号：{{item.order_id}}</text><text class="icon">{{item.type}}</text>
					</view>
					<view class="type">
						<!--text>{{item.sub_account}}</text-->
						<text class="red">{{item.status_text}}</text>
					</view>
				</view>
				<view class="goods-list" @click="goDetails(token,item.id)">
					<view class="list">
						<view class="info">
							<text class="big">操盘资金</text>
							<text class="sub">{{calculate(item.init_money)}}</text>
						</view>
						<view class="info">
							<text class="big">可用余额</text>
							<text class="sub">{{calculate(item.avail)}}</text>
						</view>
						<view class="info">
							<text class="big">操盘账户</text>
							<text class="sub blue">{{item.sub_account || '--'}}</text>
						</view>
						<view class="info">
							<text class="big">预计盈亏</text>
							<text class="sub" :class="{'red':parseInt(item.return_money)>=0}">{{calculate(item.return_money)}}</text>
						</view>
					</view>
				</view>
				<view class="order-status">
					<view class="status">
						<text>操盘时间：</text>
						<text>{{item.verify_time}}至{{item.end_time}}</text>
					</view>
					<view class="switch-setting" v-if="item.types<4 && item.status==1">
						<text class="action">自动续期</text>
						<switch class="red sm" color="red !important" v-if="item.renewal==1" checked="true" @change="onRenewal(index,item.id)" style="transform:scale(0.7)"></switch>
						<switch class="red sm" color="red !important" v-else  @change="onRenewal(index,item.id)" style="transform:scale(0.7)"></switch>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	var loginRes;
	export default {
		data() {
			return {
				OrderType: 4,
				renewal: 1,//自动续期状态
				PeiziList:[],
			};
		},
		onLoad() {
			//this.OrderType = params.type;
			loginRes = this.checkLogin();
			if(!loginRes){return;}
			//console.log(loginRes);
			this.token = loginRes[2];
			this.myMobile = loginRes[1];
			this.getPeiziList(this.token,this.OrderType,1);//获取子账号信息
		},
		methods:{
			/* 订单tab切换状态 */
			onOrderTab(type){
				this.OrderType = type;
				this.getPeiziList(this.token,type,1);//获取子账号信息
			},
			/*获取操盘信息*/
			async getPeiziList(token,status,page){
				//uni.showLoading({title:"加载中",icon:"loading"});
				this.PeiziList = '';
				if(status==0)status=null;
				uni.request({
					url: this.apiServer+'/apicom/financing?status='+status+'&type_list=search&page='+page,
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 20000,
					data:{
						token   : token,
					},
					success: res => {
						//uni.hideLoading();
						if(res.data.status==1){
							var list = res.data.data.data;
							this.PeiziList = list;
							//console.log(res.data.data);
						}
					},
					fail:function(e){
						//uni.hideLoading();
						uni.showToast({title:"加载失败!",icon:"none"});
					}
				});
				uni.hideLoading();
			},
			/*自动续期按钮*/
			async onRenewal(index,id){
				uni.request({
					url: this.apiServer+'/apicom/financing/autorenewal',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						id   : id,
						token: this.token
					},
					success: res => {
						if(res.data.status == 1){
							this.PeiziList[index].renewal  = res.data.data;
							uni.showToast({title:res.data.message,icon:"none"});
						}
					},
					fail:function(e){
						uni.showToast({title:"加载失败!",icon:"none"});
					}
				});
			},
			/*计算单位*/
			calculate(number){
				if(number==0)return number;
				if(parseInt(number).toString().length >= 13){
					var unit = (number/1000000000000).toFixed(2) + '万亿';
					return unit;
				}else if(parseInt(number).toString().length >= 9){
					var unit = (number/100000000).toFixed(2) + '亿';
					return unit;
				}else if(parseInt(number).toString().length >= 4){
					var unit =  (number/10000).toFixed(2) + '万';
					return unit;
				}else{
					var unit =  number.toFixed(2) + '元';
					return unit;
				}
			},
			goDetails(token,id){
				uni.navigateTo({
					url: '/pages/AccoutSet/AccoutSet?token='+token+'&id='+id,
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'AfterSalesOrder.scss';
</style>
